﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using gregn6Lib;
using System.Configuration;
using Common;
using System.IO;
using DevExpress.XtraEditors;
using SQLDLL;
using NursingFormSystem.KnowingAgreeBook;
using NursingFormSystem.NurFormsService;
using NursingFormSystem.Model;

namespace NursingFormSystem.KnowingAgreeBook
{
    public partial class AgreeBookPrint : Form
    {
        private GridppReport Report = new GridppReport();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();

        public AgreeBookPrint(string p1, int p2, int p3)
        {
            InitializeComponent();

            var ret = formService.GetNursingFormRecord(p1, p2, p3);
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                NursingFormRecord record1 = list[0];
                Knowing ps = XmlUtil.XMLToObject<Knowing>(record1.NursingFormData, Encoding.UTF8);

                string select = @"select top 1 (CASE SUBSTRING('" + ps.YueShuGongJu.YuanYin + @"',1,1) WHEN 0 THEN 0 ELSE 1 END) AS TSGJ_YuanYin1,(CASE SUBSTRING('" + ps.YueShuGongJu.YuanYin + @"',3,1) WHEN 0 THEN 0 ELSE 1 END) AS TSGJ_YuanYin2,(CASE SUBSTRING('" + ps.YueShuGongJu.YuanYin + @"',5,1) WHEN 0 THEN 0 ELSE 1 END) AS TSGJ_YuanYin3,(CASE SUBSTRING('" + ps.YueShuGongJu.YuanYin + @"',7,1) WHEN 0 THEN 0 ELSE 1 END) AS TSGJ_YuanYin4,'" + ps.YueShuGongJu.YY_QiTa + @"' AS TSGJ_YuanYinQT,
(CASE SUBSTRING('" + ps.YueShuGongJu.YuFang + @"',1,1) WHEN 0 THEN 0 ELSE 1 END) AS TSGJ_YuFang1,(CASE SUBSTRING('" + ps.YueShuGongJu.YuFang + @"',3,1) WHEN 0 THEN 0 ELSE 1 END) AS TSGJ_YuFang2,(CASE SUBSTRING('" + ps.YueShuGongJu.YuFang + @"',5,1) WHEN 0 THEN 0 ELSE 1 END) AS TSGJ_YuFang3,(CASE SUBSTRING('" + ps.YueShuGongJu.YuFang + @"',7,1) WHEN 0 THEN 0 ELSE 1 END) AS TSGJ_YuFang4,(CASE SUBSTRING('" + ps.YueShuGongJu.YuFang + @"',9,1) WHEN 0 THEN 0 ELSE 1 END) AS TSGJ_YuFang5,'" + ps.YueShuGongJu.YF_QiTa + @"' AS TSGJ_YuFangQT,
(CASE SUBSTRING('" + ps.YueShuGongJu.BingFaZheng + @"',1,1) WHEN 0 THEN 0 ELSE 1 END) AS TSGJ_BingFaZheng1,(CASE SUBSTRING('" + ps.YueShuGongJu.BingFaZheng + @"',3,1) WHEN 0 THEN 0 ELSE 1 END) AS TSGJ_BingFaZheng2,(CASE SUBSTRING('" + ps.YueShuGongJu.BingFaZheng + @"',5,1) WHEN 0 THEN 0 ELSE 1 END) AS TSGJ_BingFaZheng3,(CASE SUBSTRING('" + ps.YueShuGongJu.BingFaZheng + @"',7,1) WHEN 0 THEN 0 ELSE 1 END) AS TSGJ_BingFaZheng4,'" + ps.YueShuGongJu.BFZ_QiTa + @"' AS TSGJ_BingFaZhengQT,
(CASE SUBSTRING('" + ps.ErShiSiXiaoShiKanHu.YiShiZhangAi + @"',1,1) WHEN 0 THEN 0 ELSE 1 END) AS KH_YSZA1,(CASE SUBSTRING('" + ps.ErShiSiXiaoShiKanHu.YiShiZhangAi + @"',3,1) WHEN 0 THEN 0 ELSE 1 END) AS KH_YSZA2,(CASE SUBSTRING('" + ps.ErShiSiXiaoShiKanHu.YiShiZhangAi + @"',5,1) WHEN 0 THEN 0 ELSE 1 END) AS KH_YSZA3,(CASE SUBSTRING('" + ps.ErShiSiXiaoShiKanHu.YiShiZhangAi + @"',7,1) WHEN 0 THEN 0 ELSE 1 END) AS KH_YSZA4,'" + ps.ErShiSiXiaoShiKanHu.YSZA_QiTa + @"' AS KH_YSZAQT,
(CASE SUBSTRING('" + ps.ErShiSiXiaoShiKanHu.JingShenZhangAi + @"',1,1) WHEN 0 THEN 0 ELSE 1 END) AS KH_JSZA1,(CASE SUBSTRING('" + ps.ErShiSiXiaoShiKanHu.JingShenZhangAi + @"',3,1) WHEN 0 THEN 0 ELSE 1 END) AS KH_JSZA2,(CASE SUBSTRING('" + ps.ErShiSiXiaoShiKanHu.JingShenZhangAi + @"',5,1) WHEN 0 THEN 0 ELSE 1 END) AS KH_JSZA3,'" + ps.ErShiSiXiaoShiKanHu.JSZA_QiTa + @"' AS KH_JSZAQT,
(CASE SUBSTRING('" + ps.ErShiSiXiaoShiKanHu.NianLin + @"',1,1) WHEN 0 THEN 0 ELSE 1 END) AS KH_NianLin1,(CASE SUBSTRING('" + ps.ErShiSiXiaoShiKanHu.NianLin + @"',3,1) WHEN 0 THEN 0 ELSE 1 END) AS KH_NianLin2,'" + ps.ErShiSiXiaoShiKanHu.NL_QiTa + @"' AS KH_NianLinQT,
(CASE SUBSTRING('" + ps.ErShiSiXiaoShiKanHu.YuFang + @"',1,1) WHEN 0 THEN 0 ELSE 1 END) AS KH_YuFang1,(CASE SUBSTRING('" + ps.ErShiSiXiaoShiKanHu.YuFang + @"',3,1) WHEN 0 THEN 0 ELSE 1 END) AS KH_YuFang2,(CASE SUBSTRING('" + ps.ErShiSiXiaoShiKanHu.YuFang + @"',5,1) WHEN 0 THEN 0 ELSE 1 END) AS KH_YuFang3,(CASE SUBSTRING('" + ps.ErShiSiXiaoShiKanHu.YuFang + @"',7,1) WHEN 0 THEN 0 ELSE 1 END) AS KH_YuFang4,(CASE SUBSTRING('" + ps.ErShiSiXiaoShiKanHu.YuFang + @"',9,1) WHEN 0 THEN 0 ELSE 1 END) AS KH_YuFang5,(CASE SUBSTRING('" + ps.ErShiSiXiaoShiKanHu.YuFang + @"',11,1) WHEN 0 THEN 0 ELSE 1 END) AS KH_YuFang6,'" + ps.ErShiSiXiaoShiKanHu.YF_QiTa + @"' AS KH_YuFangQT,
'" + ps.JiaShuYiJian + @"' AS JiaShuYiJian,'" + ps.QianMing + @"' AS QianMing,'" + ps.YuHuanZheGuanXi + @"' AS YuHuanZheGuanXi,'" + ps.HuShiQianMing + @"' AS HuShiQianMing,'" + ps.RiQi + @"' AS RiQi,
b.PATIENT_NAME,b.SEX,b.AGE,b.NURSE_CELL_NAME,b.DEPARTMENT_NAME,b.BED_NO,b.PATIENT_ID  
              from NursingFormRecord as a left join V_YDHL_PATIENT b on a.PatientId=b.PATIENT_ID and a.VisitId=b.VISIT_ID where a.PatientId  = '" + p1 + "' and a.VisitId='" + p2 + "' and a.FormId='" + p3 + "' ";

                //设置 ShowPrintDlg 属性，让点击工具栏中的打印按钮时不显示打印对话框
                axGRPrintViewer1.ShowPrintDlg = false;

                try
                {
                    if (File.Exists(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\特殊护理措施知情同意书.grf"))
                    {
                        Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\特殊护理措施知情同意书.grf");
                    }
                    else
                    {
                        Report.LoadFromFile(@"../../Ydhl_GRF/特殊护理措施知情同意书.grf");
                    }
                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show("加载临时医嘱表单失败，请检查文件Ydhl_GRF/特殊护理措施知情同意书.grf是否存在");
                    WriteLog.WriteLogString(ex.Message + ".  加载临时医嘱表单失败，请检查文件Ydhl_GRF/Order-LZ.grf是否存在");
                }

                Report.DetailGrid.Recordset.QuerySQL = select;

                axGRPrintViewer1.Report = Report;
                axGRPrintViewer1.Start();
            }
            else if (ret.Code == 2)
            {
                string select = @"select top 1 0 AS TSGJ_YuanYin1,0 AS TSGJ_YuanYin2,0 AS TSGJ_YuanYin3,0 AS TSGJ_YuanYin4,'' AS TSGJ_YuanYinQT,0 AS TSGJ_YuFang1,0 AS TSGJ_YuFang2,0 AS TSGJ_YuFang3,0 AS TSGJ_YuFang4,0 AS TSGJ_YuFang5,'' AS TSGJ_YuFangQT,
0 AS TSGJ_BingFaZheng1,0 AS TSGJ_BingFaZheng2,0 AS TSGJ_BingFaZheng3,0 AS TSGJ_BingFaZheng4,'' AS TSGJ_BingFaZhengQT,0 AS KH_YSZA1,0 AS KH_YSZA2,0 AS KH_YSZA3,0 AS KH_YSZA4,'' AS KH_YSZAQT,
0 AS KH_JSZA1,0 AS KH_JSZA2,0 AS KH_JSZA3,'' AS KH_JSZAQT,0 AS KH_NianLin1,0 AS KH_NianLin2,'' AS KH_NianLinQT,0 AS KH_YuFang1,0 AS KH_YuFang2,0 AS KH_YuFang3,0 AS KH_YuFang4,0 AS KH_YuFang5,0 AS KH_YuFang6,'' AS KH_YuFangQT,
'' AS JiaShuYiJian,'' AS QianMing,'' AS YuHuanZheGuanXi,'' AS HuShiQianMing,'' AS RiQi,
b.PATIENT_NAME,b.SEX,b.AGE,b.NURSE_CELL_NAME,b.DEPARTMENT_NAME,b.BED_NO,b.PATIENT_ID  
              from V_YDHL_PATIENT b  where b.PATIENT_ID  = '" + p1 + "' and b.VISIT_ID='" + p2 + "' ";

                //设置 ShowPrintDlg 属性，让点击工具栏中的打印按钮时不显示打印对话框
                axGRPrintViewer1.ShowPrintDlg = false;

                try
                {
                    if (File.Exists(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\特殊护理措施知情同意书.grf"))
                    {
                        Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\特殊护理措施知情同意书.grf");
                    }
                    else
                    {
                        Report.LoadFromFile(@"../../Ydhl_GRF/特殊护理措施知情同意书.grf");
                    }
                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show("加载临时医嘱表单失败，请检查文件Ydhl_GRF/特殊护理措施知情同意书.grf是否存在");
                    WriteLog.WriteLogString(ex.Message + ".  加载临时医嘱表单失败，请检查文件Ydhl_GRF/Order-LZ.grf是否存在");
                }

                Report.DetailGrid.Recordset.QuerySQL = select;

                axGRPrintViewer1.Report = Report;
                axGRPrintViewer1.Start();
            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
        }
    }
}
